

import 'package:basic_common/common_exports.dart';
import 'package:basic_patient/model/patient_single_data.dart';

class PatientHealthController extends GetxController
    with GetSingleTickerProviderStateMixin {
  final basicInfoList = [
    {"attr": "头像", "value": ""},
    {"attr": "真实姓名", "value": ""},
    {"attr": "身份证号", "value": ""},
    {"attr": "联系电话", "value": ""},
    {"attr": "现居地", "value": ""},
    {"attr": "户籍地址", "value": ""},
    {"attr": "行政区划", "value": ""},
    {"attr": "患者标签", "value": ""},
    {"attr": "性别", "value": ""},
    {"attr": "出生日期", "value": ""},
    {"attr": "籍贯", "value": ""},
    {"attr": "民族", "value": ""},
    {"attr": "文化程度", "value": ""},
    {"attr": "婚姻状况", "value": ""},
    {"attr": "职业", "value": ""},
    {"attr": "工作单位", "value": ""},
    {"attr": "紧急联系人", "value": ""},
    {"attr": "联系人电话", "value": ""},
    {"attr": "户籍类型", "value": ""},
    {"attr": "定点医疗单位", "value": ""},
    {"attr": "费用类型", "value": ""}
  ].obs;

  //健康信息
  final heathInfoList = [
    {"attr": "身高", "value": ""},
    {"attr": "体重", "value": ""},
    {"attr": "血型", "value": ""},
    {"attr": "RH阴性", "value": ""},
  ].obs;
  //生活习惯
  List habitList = [
    {"attr": "饮食习惯", "value": ""},
    {"attr": "运动习惯", "value": ""},
    {"attr": "睡眠质量", "value": ""},
    {"attr": "饮酒习惯", "value": ""},
    {"attr": "久坐习惯", "value": ""},
    {"attr": "心理情绪", "value": ""},
  ];
  //健康史
  final healthHistoryList = [
    {"attr": "过敏史", "value": ""},
    {"attr": "既往史", "value": ""},
    {"attr": "家族史", "value": ""},
    {"attr": "用药史", "value": ""},
  ].obs;
  String consumeNo = Get.arguments;

  final pData = PatientSingleData().obs;
  final age = '0'.obs;
  TabController? tabController;
  final todoTitleList = ["基础信息", "健康信息"];
  String defStr = '';

  @override
  void onInit() {
    // TODO: implement onInit
    tabController = TabController(length: todoTitleList.length, vsync: this);
    getPatientDetail();

    super.onInit();
  }

  /// 获取详情
  getPatientDetail() async {
    HttpData res = await ApiService().getPatientDetail(data: {
      "scene": "get-by-patientNo",
      "sceneDesc": "根据patientNo查询患者",
      "searchKey": "PatientNo",
      "searchValue": consumeNo
    });
    if (res.code == 1) {
      pData.value = PatientSingleData?.fromJson(res.data);
      PatientSingleData itemData = pData.value;

      updateInfo(0, itemData.avatarUrl ?? '');
      updateInfo(1, itemData.patientName ?? defStr);
      updateInfo(2, itemData.idCardNo ?? defStr);

      updateInfo(3, itemData.phone ?? defStr);

      updateInfo(4, itemData.address ?? defStr);
      updateInfo(5, itemData.domicile ?? defStr);
      updateInfo(6, itemData.areaText ?? defStr);
      updateInfo(7, itemData.tagsFromDoctor ?? defStr);

      updateInfo(8, itemData.gender == '1' ? '男' : '女');
      updateInfo(9, itemData.birthday ?? defStr);
      updateInfo(10, itemData.domicile ?? defStr);
      updateInfo(11, itemData.nation ?? defStr);

      updateInfo(12, itemData.educationLevel ?? defStr);

      updateInfo(13, itemData.marital == '1' ? '已婚' : '未婚');

      updateInfo(14, itemData.work ?? defStr);
      updateInfo(15, itemData.workUnit ?? defStr);

      updateInfo(16, itemData.emergencyContact ?? defStr);
      updateInfo(17, itemData.emergencyContact ?? defStr); //联系人电话

      updateInfo(18, itemData.domicileType ?? defStr);

      updateInfo(19, itemData.medicalUnit ?? defStr);
      updateInfo(20, medicaTypeToStr(itemData.medicalInsuranceType??'') );

      // 健康信息
      updateHealthInfo(0, itemData.height ?? defStr);
      updateHealthInfo(1, itemData.weight ?? defStr);
      updateHealthInfo(2, blodTypeToStr(itemData.bloodType??''));
      updateHealthInfo(3, itemData.rhYx == '1' ? '是' : '不是');

      // 生活习惯
      updatehabitInfo(0, itemData.habitDietary ?? defStr);
      updatehabitInfo(1, itemData.habitExercise ?? defStr);

      updatehabitInfo(2, itemData.habitSleep ?? defStr);

      updatehabitInfo(3, drinkTypeToStr(itemData.drinkingStatus??'')); //烟酒习惯

      updatehabitInfo(4, itemData.habitSit ?? defStr);
      updatehabitInfo(5, itemData.habitEmotion ?? defStr);

      //健康史
      updateHealthHistroyInfo(0, itemData.historyAllergic ?? defStr);
      updateHealthHistroyInfo(1, itemData.historyMedical ?? defStr);
      updateHealthHistroyInfo(2, itemData.historyFamily ?? defStr);
      updateHealthHistroyInfo(3, itemData.historyMedication ?? defStr);
    }
    update(['patientInfo']);
  }

// 血型
  String blodTypeToStr(String type) {
    String blodStr = defStr;
    switch (type) {
      case '1':
        blodStr = 'A';
        break;
      case '2':
        blodStr = 'B';
        break;
      case '3':
        blodStr = 'O';
        break;
      case '4':
        blodStr = 'AB';
        break;
      case '5':
        blodStr = '其他';
        break;
    }
    return blodStr;
  }

  // 医疗类型
  String medicaTypeToStr(String type) {
    String medicaStr = defStr;
    switch (type) {
      case '1':
        medicaStr = '新农合';
        break;
      case '2':
        medicaStr = '市医保';
        break;
      case '3':
        medicaStr = '省医保';
        break;
    }
    return medicaStr;
  }

  // 饮酒习惯
  String drinkTypeToStr(String type) {
    String drinkStr = defStr;
    switch (type) {
      case '0':
        drinkStr = '不喝酒';
        break;
      case '1':
        drinkStr = '偶尔饮酒';
        break;
      case '2':
        drinkStr = '经常喝酒';
        break;
    }
    return drinkStr;
  }

  // bae 更新数据
  updateInfo(int index, String value) {
    basicInfoList[index]['value'] = value;
  }

// 生活习惯
  updatehabitInfo(int index, String value) {
    habitList[index]['value'] = value;
  }

  updateHealthInfo(int index, String value) {
    heathInfoList[index]['value'] = value;
  }
  // 健康史

  updateHealthHistroyInfo(int index, String value) {
    healthHistoryList[index]['value'] = value;
  }
}
